import * as fs from 'fs';

export class ConfigEditor {

    initView() {
        const container = document.getElementById("configeditor");

        const files = ['assets/json/missions.json'];
        // const files = fs.readdirSync(dir).map(filename => dir + "/" + filename);
        console.log(files);


        for (let filepath of files) {
            const content = fs.readFileSync(filepath, 'utf-8')
            const json = JSON.parse(content);
            // console.log(json);

            const metadatas = json.metadata || [];

            for (let item of json.data) {
                // if(!json.data){
                //     continue;
                // }
                for (let metadata of metadatas) {
                    const header = document.createElement("div");
                    container.appendChild(header);
                    const input = document.createElement("input");
                    container.appendChild(input)

                    header.innerText = metadata.title;
                    input.value = item[metadata.key];
                    input.onchange = () => {
                        item[metadata.key] = input.value;
                    }
                    input.disabled = !metadata.editable
                }
            }

            const button = document.createElement('button');
            button.innerText = "保存"
            container.appendChild(button);
            button.onclick = () => {
                const conent = JSON.stringify(json, null, '\t');
                fs.writeFileSync(filepath, conent, "utf-8");
            }
        }
    }
}